<?php
require_once '../DTO/TinRaoVat.php';
require_once 'DBConnection.php';

class TinRaoVatDAO extends DBConnection{
	
	public function __construct(){
		parent::__construct();
	}
	
	//Lấy danh sách tất cả các tin rao vặt
	public function getTinRaoVat(){
		$sql = "select * from tinraovat";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy danh sách tất cả tin rao vặt sắp xếp theo thời gian giảm dần
	public function getTinRaoVatSortByTime(){
		$sql = "select * from tinraovat order by NgayRao DESC";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy tất cả tin rao vặt sắp xếp theo giá giảm dần
	public function getTinRaoVatSortByGiaDESC(){
		$sql = "select * from tinraovat order by GiaCa DESC";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy tất cả tin rao vặt sắp xếp theo giá tăng dần
	public function getTinRaoVatSortByGiaASC(){
		$sql = "select * from tinraovat order by GiaCa ASC";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy tất cả tin rao vặt theo mã người dùng
	public function getTinRaoVatByMaND($maND){
		$sql = "select * from tinraovat where MaND='$maND'";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy tất cả tin rao vặt theo mã sản phẩm
	public function getTinRaoVatByMaSP($maSP){
		$sql = "select * from tinraovat where MaSP='$maSP'";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}

	//Lấy tất cả tin rao vặt theo mã vùng
	public function getTinRaoVatByMaVung($maVung){
		$sql = "SELECT * FROM tinraovat WHERE MaTRV in (select MaTRV from chitiettinhraovat 
				where MaTinh in (select MaTinh from Vung where MaVung='$maVung'))";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy tất cả tin rao vặt theo mã tỉnh
	public function getTinRaoVatByMaTinh($maTinh){
		$sql = "SELECT * FROM tinraovat WHERE MaTRV in (select MaTRV from chitiettinhraovat 
				where MaTinh='$maTinh')";
	
		$res = mysql_query($sql, $this->hDB);
	
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
	
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy tin rao vặt theo mã tin
	public function getTinRaoVatByMaTRV($maTRV){
		$sql = "SELECT * FROM tinraovat WHERE MaTRV='$maTRV'";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){			
			$temp = mysql_fetch_assoc($res);			
			$obj = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
			return $obj;
		}else{
			return null;
		}
	}
	
	//Lấy danh sách tin rao vặt theo phân trang
	public function getTinRaoVatPaging($index, $number){
		$sql = "SELECT * FROM tinraovat limit $index,$number";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], $temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Tìm tin rao vặt theo mã danh mục sản phẩm
	public function getTinRaoVatByMaDMSP($maDMSP){
		$sql = "select * from tinraovat where MaSP in (select MaSP from sanpham where MaDMSP=$maDMSP)";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!($res && mysql_num_rows($res))){
			return NULL;
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$objs[] = new TinRaoVat($temp['MaTRV'], $temp['MaLoaiTRV'], $temp['TieuDe'], $temp['GiaCa'], $temp['NoiDung'], 
						$temp['HinhAnh'], $temp['NgayRao'], $temp['SoLuongTruyCap'], $temp['MaND'], $temp['MaSP']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Insert into tinraovat
	public function insert(TinRaoVat $tinraovat){
		$maLoaiTRV = $tinraovat->getMaLoaiTRV();
		$tieuDe = $tinraovat->getTieuDe();
		$giaCa = $tinraovat->getGiaCa();
		$noiDung = $tinraovat->getNoiDung();
		$hinhAnh = $tinraovat->getHinhAnh();
		$ngayRao = $tinraovat->getNgayRao();
		$soLuongTruyCap = $tinraovat->getSoLuongTruyCap();
		$maND = $tinraovat->getMaND();
		$maSP = $tinraovat->getMaSP();
		
		$sql = "insert into tinraovat (MaLoaiTRV, TieuDe, GiaCa, NoiDung, HinhAnh, 
		NgayRao, SoLuongTruyCap, MaND, MaSP) values ('$maLoaiTRV', '$tieuDe', '$giaCa', '$noiDung', '$hinhAnh',
		'$ngayRao', '$soLuongTruyCap', '$maND', '$maSP')";
		
		$res = mysql_query($sql);
		
		if(!$res){
			die("Faile insert into tinraovat");
		}
	}

}



















